<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>推荐管理</title>
<link rel="stylesheet" type="text/css"
    href="../../js/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
    href="../../js/easyui/themes/icon.css">
<link href="../../js/uploadify/uploadify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../../js/easyui/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../../js/uploadify/swfobject.js"></script>
<script type="text/javascript" src="../../js/uploadify/jquery.uploadify.v2.1.4.min.js"></script>
<script type="text/javascript"
    src="../../js/easyui/jquery.easyui.min.js"></script>
<script>
function createReommend() {
    $("#recommendPhotoForm").form('submit', {
        url: "add_or_update_recommend_photo.action",
        onSubmit:function() {
            return $(this).form('validate');
        },
        success:function(data) {
            $('#recommendPhotosGrid').datagrid('reload');
            closeReommendForm();
        }
    });
}

function closeReommendForm() {
    $('#recommendPhotoForm').form('clear');
    $('#itemEditorDialog').dialog('close');
}


function addReommend() {
	alert($('body').html());
    $('#recommendPhotoForm').form('load', {
        'recommendPhoto.id':-1
    });
    $('#itemEditorDialog').show();
    $('#itemEditorDialog').dialog({
        title:'添加推荐',
        modal:true
    });
}

function editReommendLink() {
    var node = $('#recommendPhotosGrid').datagrid('getSelected');
    var rows = $('#recommendPhotosGrid').datagrid('getSelections');

    if (rows.length != 1) {
        $.messager.alert('警告', '请选择一条记录!', 'warning');
        return;
    }

    $('#recommendPhotoForm').form('load', {
        'recommendPhoto.id':node.id,
        'recommendPhoto.name':node.name,
        'recommendPhoto.link':node.link
    });

    $('#itemEditorDialog').show();
    $('#itemEditorDialog').dialog({
        name:'编辑',
        modal:true
    });
}

function editReommendPhoto() {
    var node = $('#recommendPhotosGrid').datagrid('getSelected');
    var rows = $('#recommendPhotosGrid').datagrid('getSelections');

    if (rows.length != 1) {
        $.messager.alert('警告', '请选择一条记录!', 'warning');
        return;
    }

    $('#recommendPhotoId').val(node.id);
    var inti = $('#uploadInit').val();
    if(inti =="0"){
        var html=$('#photoDialog').html();
        $('#photoDialog').dialog({        
            modal: true          
                   });
         $('#photoDialog').empty().append($(html));   
         $('#uploadify').uploadify(
                 {
                     'uploader' : '../../js/uploadify/uploadify.swf',
                     'script' : 'add_or_update_recommended_photo.action',
                     'scriptData' : {
                         'recommendPhoto.id' : $('#recommendPhotoId').val()
                     },
                     'cancelImg' : '../../js/uploadify/cancel.png',
                     'folder' : '../../gallery/',
                     'fileDataName' : 'uploadify',
                     'auto' : true,
                     'onComplete' : function(event, ID, fileObj,
                             response, data) {
                    	 $('#recommendPhotosGrid').datagrid('reload');
                     }
                 });  
         $('#uploadInit').val("1");
    } else{
        $('#photoDialog').dialog('open');
    }
}

function deleteReommend() {
    var node = $('#recommendPhotosGrid').datagrid('getSelected');
    var rows = $('#recommendPhotosGrid').datagrid('getSelections');

    if (rows.length != 1) {
        $.messager.alert('警告', '请选择一条记录!', 'warning');
        return;
    }
    $('#recommendPhotoForm').form('load', {
        'recommendPhoto.id':node.id,
        'recommendPhoto.name':node.name
    });
    $.messager.confirm('确认','确认要删除吗?',function(fn){   
        if (fn){            
            $("#recommendPhotoForm").form('submit', {
                url: "delete_recommendPhoto.action",
                success:function(data) {
                    $('#recommendPhotosGrid').datagrid('reload');
                }
            });   
        }   
    });  
}

$(function() {
    $('#recommendPhotosGrid').datagrid( {
            url : 'find_recommend_photo.action',
            idField:'id',
    
            columns : [ [ 
                {field: 'id', title: '序号', width: 80},
                {field: 'name', title: '名字', width: 220, height: 220, formatter: function(value, rowData){                
                    return "<img width='200' src='../../gallery/recommend/" + value + "' />";
                }},
                {field: 'link', title: '链接', width: 300}
            ] ],
            toolbar:[
                        {
                            text:'添加',
                            iconCls:'icon-add',
                            handler:function() {
                                addReommend();
                            }
                        },
                        {
                            text:'编辑图片',
                            iconCls:'icon-edit',
                            handler:function() {
                                editReommendPhoto();
                            }
                        },
                        {
                            text:'编辑链接',
                            iconCls:'icon-edit',
                            handler:function() {
                                editReommendLink();
                            }
                        },
                        '-',
                        {
                            text:'删除',
                            iconCls:'icon-remove',
                            handler:function() {
                                deleteReommend();
                            }
                        }
                   ]
    });
    
});
</script>
</head>
<body>

<h1>DataGrid</h1>
<table id="recommendPhotosGrid"></table>

<div id="photoDialog" icon="icon-save" title="管理"
    style="height: 400; width: 400; display: none; padding: 5px;">
    <form>
        <input id="uploadify" name="uploadify" type="file" />
        <input id="recommendPhotoId" name="recommendPhotoId" value=''/>  
        <input id="uploadInit" name="uploadInit"  value="0"></input>
    </form>
</div>

<div id="itemEditorDialog" icon="icon-save" title="管理"
    style="height: 400; width: 400; display: none; padding: 5px;">
<form method="post" id="recommendPhotoForm" name="recommendPhotoForm">
<table>
    <tr>
        <td><label>链接：</label></td>
        <td><input class="easyui-validatebox" id="link"
            name="recommendPhoto.link"/></td>
    </tr>
</table>
<div class="toolbar" style="padding: 10px;"><a href="#"
    onclick="createReommend()" class="easyui-linkbutton" iconCls="icon-ok">确定</a>
<a href="#" onclick="closeReommendForm()" class="easyui-linkbutton"
    iconCls="icon-cancel">取消</a></div>
<input type="hidden" name="recommendPhoto.id" value="-1" />
<input type="hidden" name="recommendPhoto.name" value="-1" /></form>
</div>
</body>
</html>